package com.leslie.springboottest.handler;

import com.leslie.springboottest.data.PersonResponse;
import com.leslie.springboottest.enums.PersonResponseEnum;
import com.leslie.springboottest.excption.PersonException;
import com.leslie.springboottest.util.PersonResponseUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class PersonHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public PersonResponse handle(Exception e) {
        if (e instanceof PersonException){
            PersonException personException = (PersonException) e;
            return PersonResponseUtil.error(personException);
        } else {
            return PersonResponseUtil.error(PersonResponseEnum.UNKNOW_ERR);
        }
    }
}
